package com.wutu.nbyjk8.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class GeoStrGeneratorUtils {

    private static ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper){
        GeoStrGeneratorUtils.objectMapper = objectMapper;
    }



    public  static String getStr(String geoType, Object geoParam ){
         List<List<List<Double>>> pointList = null ;
        if(null != geoParam){
            pointList = objectMapper.convertValue(geoParam,List.class);
        }
        List<String> finalGeoInfo = new ArrayList<>();
        String itemStr = "";
        if(null != pointList && !pointList.isEmpty()){
            if(pointList.size()>1){
                itemStr = "MULTIPOLYGON(";
                String pointStr = "";
                Integer parentListKey = 1;
                for (List<List<Double>> pointItem : pointList){
                    pointStr= "((";
                    Integer key = 1;
                    for(List<Double> point: pointItem){
                        pointStr = pointStr + point.get(0) +" "+ point.get(1);
                        if(key < pointItem.size()){
                            pointStr += ",";
                        }
                        key++;
                    }
                    pointStr+="))";
                    if(parentListKey < pointList.size()){
                        pointStr+=",";
                    }
                    parentListKey++;
                    itemStr+=pointStr;
                }
                itemStr+=")";

            }else{
                itemStr = "POLYGON(";
                String pointStr = "";
                for (List<List<Double>> pointItem : pointList){
                    pointStr= "(";
                    Integer key = 1;
                    for(List<Double> point: pointItem){
                        pointStr = pointStr + point.get(0) +" "+ point.get(1);
                        if(key < pointItem.size()){
                            pointStr += ",";
                        }
                        key++;
                    }
                    pointStr+=")";
                }
                itemStr+=pointStr;
                itemStr+=")";
            }

        }
        return itemStr;
    }
}
